< previous page page_86 next page >

Page 86
e10a6a0a59f035e870d90cca05b85fce.gif
Private Declare Function SetBitmapBits Lib "gdi32" _
    Alias "SetBitmapBits" (ByVal hBitmap As Long, _
    ByVal dwCount As Long, lpBits As Any) As Long
You can use GetBitmapBits to transfer pixel data from a bitmap into an array. You can then manipulate the data in the array and use SetBitmapBits to transfer the data back into the bitmap when you are finished. If you need to modify many of the values in the array, this is much faster than using Visual Basic's Point and PSet routines.
Unfortunately, the API documentation says that GetBitmapBits and SetBitmapBits are obsolete and are included for compatibility with 16-bit applications. These functions are still provided in Windows NT, an operating system that cannot run 16-bit applications, so it seems there is no big hurry to remove these functions from use. They are also faster and easier to use than the device-independent bitmap (DIB) routines that are designed to replace them. Later sections have more to say about DIB functions. If these functions really do become extinct in later versions of the Windows operating systems, look at this book's Web page (www.vb-helper.com/vbgp.htm) for updates.
The following code uses GetObject to obtain information about the bitmap contained in the picture box picSource. It uses the bitmap's width in bytes and its height to create an array large enough to hold the bitmap's pixel data. It then calls GetBitmapBits to copy the pixel data into the array. After manipulating the data in the array, in this case giving each pixel the color value 0, the code uses SetBitmapBits to copy the pixels back into the bitmap.
e10a6a0a59f035e870d90cca05b85fce.gif
Dim bm As BITMAP
Dim bytes() As Byte
Dim i As Integer
Dim j As Integer
Dim wid As Integer
Dim hgt As Integer

    ' Get the basic bitmap information to see how big it is.
    GetObject picSource.Image, Len(bm), bm

    ' Get the pixels.
    wid = bm.bmWidthBytes
    hgt = bm.bmHeight
    ReDim bytes(1 To wid, 1 To hgt)
    GetBitmapBits picSource.Image, wid * hgt, bytes(1, 1)

    ' Set all bits to color 0.
    For i = 1 To hgt
        For j = 1 To wid
            bytes(i, j) = 0
        Next j
    Next i

    ' Update the bitmap. 
    SetBitmapBits hbm, wid * hgt, bytes(1, 1)

 
< previous page page_86 next page >